package com.cs.framework.web.view.support;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cs.framework.common.util.CommonUtils;
import com.cs.framework.common.util.IOUtils;
import com.cs.framework.common.util.JSONUtils;
import com.cs.framework.core.context.Application;
import com.cs.framework.web.view.View;
import com.cs.framework.web.view.ViewInfo;

/**
 * JSON视图
 * 
 * @author hedongzhou
 * 
 */
public class JsonView implements View {

	/**
	 * 初始化
	 * 
	 * @param application
	 *            应用上下文
	 */
	public void init(Application application) {
	}

	/**
	 * 渲染视图
	 * 
	 * @param request
	 * @param response
	 * @param viewInfo
	 * @return
	 */
	public ViewInfo render(HttpServletRequest request, HttpServletResponse response, ViewInfo viewInfo) {
		response.setContentType("text/html;charset=UTF-8");

		final ViewInfo tempInfo = viewInfo;

		IOUtils.responsePrint(response, new IOUtils.ResponsePrintDeal() {
			public void doPrint() {
				if (CommonUtils.isEmpty(tempInfo.getViewData())) {
					print("");
				} else {
					print(JSONUtils.toString(tempInfo.getViewData()));
				}
			}
		});

		return null;
	}

}
